<?php
namespace Home\Controller;
use Think\Controller;

//报告
class ReportserviceController extends CommonController {
	private $report_attachment_path = "/Data/report_attachement/";
	private $auth_status = 0;//默认：未认证
    private $user_email = null;//用户邮箱
    private $uid = null;
    private $auth_type = 1;
    private $user_name = '';
    private $white_list = array('shipeng.hou@37degree.com');

    public function __construct(){
        //是否服务商用户，且登录状态
        parent::__construct();

		//判断用户是否服务商用户
        if($this->role==1){
            redirect('/Index/index');
        }
        
        $this->getAuthStatus();
        $this->auth_status = session('37degree_auth_status');
        
        $this->user_email = session('user_email');
        $this->uid = session('uid');
        $this->auth_type = session('auth_type');
        $this->user_name = session('user_name');

        $this->assign('user_email',$this->user_email);
        if(empty($this->uid)) redirect('/member/login.html');
    }

	// 我的报告列表
    public function index(){
    	// $content = '&amp;lt;p&amp;gt;dafadsfasdfads&amp;lt;/p&amp;gt;';
    	// echo stripslashes( htmlspecialchars_decode($content));die;
    	$this->display();
    }

	//发布报告
	public function add(){
		if(IS_AJAX){//新增报告
			//检测提交数据
			$data = $this->checkData(I('post.'));

			try{
				$reportModel = D('Report');
				$data['uid'] = $this->uid;
				$result = $reportModel->insertReport($data);
				if($result){
					$this->ajaxReturn( array('status'=>200,'msg'=>'OK','data'=>$result) );
				}else{
					$this->ajaxReturn( array('status'=>4001,'msg'=>'添加失败','data'=>null) );
				}
			}catch(exception $e){
				$this->ajaxReturn( array('status'=>4001,'msg'=>'网络出现问题','data'=>null) );
			}
			
		}else{
			//相关服务数据
			$related_list = $this->relatedData();
			$this->assign('related_list',$related_list);
			//timp($related_list);die;
			$this->display();
		}
		
	}
	
	//编辑报告
	public function edit(){
		if(IS_AJAX){//更新
			//检测提交数据
			$data = $this->checkData(I('post.'));
			try{
				$reportModel = D('Report');
				$data['uid'] = $this->uid;
				$result = $reportModel->updateReport($data);
				if($result){
					$this->ajaxReturn( array('status'=>200,'msg'=>'OK','data'=>$result) );
				}else{
					$this->ajaxReturn( array('status'=>4001,'msg'=>'更新失败','data'=>null) );
				}
			}catch(exception $e){
				$this->ajaxReturn( array('status'=>4001,'msg'=>'网络出现问题','data'=>null) );
			}
		}else{
			$id = I('get.id',0,'intval');
			if($id && is_numeric($id)){
				//获取数据
				$ReportModel = M('Report');
				$where['id'] = $id;
				$where['uid'] = $this->uid;
				$info = $ReportModel->where($where)->find();
				if(empty($info)) redirect("/reportservice/add.html");
				
				//相关服务数据
				$related_list = $this->relatedData();
//				timp($related_list);die;
				$this->assign('related_list',$related_list);
				$this->assign('info',$info);
				$this->display();
			}else{
				redirect("/reportservice/add.html");
			}
				
		}
		
	}

	//删除报告(下架)
	public function ajaxRemoved(){
		$id = I('post.id',0,'intval');
		$uid = $this->uid;

		$data['id'] = $id;
		$data['uid'] = $uid;
		$data['status'] = -1;
		$reportModel = D('Report');
		$result = $reportModel->updateReport($data);
		if($result){
			$this->ajaxReturn( array('status'=>200,'msg'=>'OK','data'=>$result) );
		}else{
			$this->ajaxReturn( array('status'=>4001,'msg'=>'下架失败','data'=>null) );
		}
	}

	// ajax-我的报告列表
	public function ajaxReportList(){
		$page = I('post.page',1,'intval');
    	$pagesize = I('post.pagesize',20,'intval');
    	$uid = $this->uid;
    	
    	// $uid = 7;
    	$reportModel = D('Report');
    	$list = $reportModel->getListWithUser($uid,$page,$pagesize);
    	$this->ajaxReturn( array('status'=>200,'msg'=>'OK','data'=>$list) );
	}

	//检测数据
	private function checkData($data){
		//标题检测
		if(!isset($data['title']) || empty($data['title'])) $this->ajaxReturn( array('status'=>4001,'msg'=>'报告标题不能为空','data'=>"_title") ); 
		//报告简介
		if(!isset($data['desc']) || empty($data['desc'])) $this->ajaxReturn( array('status'=>4001,'msg'=>'报告简介不能为空','data'=>"_desc") );
		//产品图片
		if(!isset($data['cover_img']) || empty($data['cover_img'])) $this->ajaxReturn( array('status'=>4001,'msg'=>'请上传封面图片','data'=>"_cover_img") );
		//附件地址
		if(!isset($data['attachment']) || empty($data['attachment'])) $this->ajaxReturn( array('status'=>4001,'msg'=>'请上传封面图片','data'=>"_attachment") );
		//详细内容--获取富文本编辑器中内容并对字符进行编码过滤
		if(!isset($data['content']) || empty($data['content'])) $this->ajaxReturn( array('status'=>4001,'msg'=>'请上传封面图片','data'=>"_content") );
		$data['content'] = htmlspecialchars(addslashes($data['content']));
		// //相关数据，以逗号分隔
		// if(isset($data['related_service_ids']) && empty($data['related_service_ids'])){
		// 	//是数组？

		// 	//字符串?
			
		// }
		// unset($data['related_service_ids']);
		//价格
		// if(!isset($data['price'])) $this->ajaxReturn( array('status'=>4001,'msg'=>'报告标题不能为空','data'=>"_price") ); 

		// $this->ajaxReturn( array('status'=>$code,'data'=>null,'msg'=>$msg) );
		return $data;

	}

	//相关数据
	public function relatedData(){
		$uid = $this->uid;
		if(empty($uid)) return null;

		$serviceModel = D('Services');
		$related_service_list = $serviceModel->getServiceWithRelatedUser($uid);
		return $related_service_list;
	}


}